package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Hbjl;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class HbjlForm extends BasePage implements Serializable {
    private GenericManager<Hbjl, Long> hbjlManager;
    private Hbjl hbjl = new Hbjl();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setHbjlManager(GenericManager<Hbjl, Long> hbjlManager) {
        this.hbjlManager = hbjlManager;
    }

    public Hbjl getHbjl() {
        return hbjl;
    }

    public void setHbjl(Hbjl hbjl) {
        this.hbjl = hbjl;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        hbjlManager.remove(hbjl.getId());
        addMessage("hbjl.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            hbjl = hbjlManager.get(id);
        } else {
            hbjl = new Hbjl();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (hbjl.getId() == null || hbjl.getId() == 0);
        hbjl.setJbqk( jbqk );
        hbjl.setUsername(getRequest().getRemoteUser());
        hbjl.setOptdate(new Date());
        hbjl.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        hbjlManager.save(hbjl);

        String key = (isNew) ? "hbjl.added" : "hbjl.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = hbjl.getId();
            return "edit";
        }
    }
} 